{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "628914a8-245d-4f76-9c0c-161c596b5338",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone as fo\n",
    "import fiftyone.zoo as foz\n",
    "import fiftyone as fo\n",
    "import fiftyone.zoo as foz\n",
    "import fiftyone.zoo as foz\n",
    "from fiftyone import ViewField as F\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1e2d17b6-639e-4565-b421-f1cacc1899dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# origin_dict = {\n",
    "#     0: \"car\",\n",
    "#     1: \"van\",\n",
    "#     2: \"truck\",\n",
    "#     3: \"tram\",\n",
    "#     4: \"person\",\n",
    "#     5: \"person_sitting\",\n",
    "#     6: \"cyclist\",\n",
    "#     7: \"misc\",\n",
    "#     8: \"aeroplane\",\n",
    "#     9: \"bicycle\",\n",
    "#     10: \"bird\",\n",
    "#     11: \"boat\",\n",
    "#     12: \"bottle\",\n",
    "#     13: \"bus\",\n",
    "#     14: \"cat\",\n",
    "#     15: \"chair\",\n",
    "#     16: \"cow\",\n",
    "#     17: \"diningtable\",\n",
    "#     18: \"dog\",\n",
    "#     19: \"horse\",\n",
    "#     20: \"motorbike\",\n",
    "#     21: \"pottedplant\",\n",
    "#     22: \"sheep\",\n",
    "#     23: \"sofa\",\n",
    "#     24: \"train\",\n",
    "#     25: \"tvmonitor\",\n",
    "#     26: \"pedestrian\",\n",
    "#     27: \"tricycle\",\n",
    "#     28: \"awning-tricycle\"\n",
    "# }\n",
    "origin_dict = {\n",
    "    0: \"Car\",\n",
    "    1: \"Van\",\n",
    "    2: \"Truck\",\n",
    "    3: \"Tram\",\n",
    "    4: \"Person\",\n",
    "    5: \"Person_sitting\",\n",
    "    6: \"Cyclist\",\n",
    "    7: \"Misc\",\n",
    "    8: \"Aeroplane\",\n",
    "    9: \"Bicycle\",\n",
    "    10: \"Bird\",\n",
    "    11: \"Boat\",\n",
    "    12: \"Bottle\",\n",
    "    13: \"Bus\",\n",
    "    14: \"Cat\",\n",
    "    15: \"Chair\",\n",
    "    16: \"Cow\",\n",
    "    17: \"Diningtable\",\n",
    "    18: \"Dog\",\n",
    "    19: \"Horse\",\n",
    "    20: \"Motorbike\",\n",
    "    21: \"Pottedplant\",\n",
    "    22: \"Sheep\",\n",
    "    23: \"Sofa\",\n",
    "    24: \"Train\",\n",
    "    25: \"Tvmonitor\",\n",
    "    26: \"Pedestrian\",\n",
    "    27: \"Tricycle\",\n",
    "    28: \"Awning-tricycle\"\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4dba484c-0719-46d2-b04e-04254ca707a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_classes = [value for k,value in origin_dict.items()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "13f77a32-48aa-4d21-93a9-0f249712fdc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "Extra_classes = [\"Person\", \"Billboard\", \"Bicycle\", \"Bird\", \"Boat\", \"Rabbit\", \"Sheep\", \"Truck\",\n",
    "           \"Van\", \"Monkey\", \"Pig\", \"Toy\", \"Traffic light\", \"Traffic sign\", \"Bus\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "41ec88dd-f761-4a0f-94ec-58dde5d40ce2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading split 'validation' to '/root/fiftyone/open-images-v6/validation' if necessary\n",
      "Found 1 images, downloading the remaining 5999\n",
      " 100% |█████████████████| 5999/5999 [2.0m elapsed, 0s remaining, 1.1 files/s]       \n",
      "Dataset info written to '/root/fiftyone/open-images-v6/info.json'\n",
      "Loading 'open-images-v6' split 'validation'\n",
      " 100% |███████████████| 6000/6000 [1.3m elapsed, 0s remaining, 79.9 samples/s]      \n",
      "Dataset 'open-images-v6-validation-6000' created\n"
     ]
    }
   ],
   "source": [
    "\n",
    "dataset = foz.load_zoo_dataset(\n",
    "    \"open-images-v6\",\n",
    "    split = \"validation\",\n",
    "    max_samples=6000,\n",
    "    seed=51,\n",
    "    shuffle=True,\n",
    "    label_types=[\"detections\"],\n",
    "    classes = Extra_classes,\n",
    "    #only_matching=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0956a935-ebac-4b83-9b91-9e11a06e4872",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2d7be792-2b87-4996-94d2-ed1b8ffe8542",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_dict = origin_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "940fd4f1-4e0e-4b9a-b320-617f703cdf3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for c in Extra_classes:\n",
    "    if c not in origin_classes:\n",
    "        new_dict[len(new_dict)] = c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9ef8b90c-106e-4abb-9037-6aa7f166e5cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_classes = [value for k,value in new_dict.items()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3e0a5211-900e-4f92-ad24-0a8c2ac57fce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(new_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bbe883b-4f6c-4016-82e3-eefe1804c930",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "daf3b8ff-c1bb-446b-bc6b-0dcdc7c376e7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37412622-7df2-4d08-a6b9-73877760ed99",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0a038896-e52d-4ec9-b93a-9322b7575f47",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = dataset.take(int(0.7 * len(dataset)), seed=42)\n",
    "val_set = dataset.exclude(train_set).take(int(0.2 * len(dataset)), seed=42)\n",
    "test_set = dataset.exclude(train_set).exclude(val_set)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a4a310b4-28df-458f-a4e7-03159d27b7ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 100% |███████████████| 4200/4200 [24.7s elapsed, 0s remaining, 203.5 samples/s]      \n"
     ]
    }
   ],
   "source": [
    "\n",
    "train_set.export(\n",
    "    export_dir=\"../openimages/train\",\n",
    "    dataset_type=fo.types.YOLOv5Dataset,\n",
    "    label_field=\"ground_truth\",  # 标签字段名\n",
    "    classes=new_classes,\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9525b9cf-b92b-4c6f-957a-8aca5d19cd54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                                                      \r"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Butterfly' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                                                      \r"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Common sunflower' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  49% |███████/-------|  590/1200 [3.9s elapsed, 3.7s remaining, 177.1 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Tortoise' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Sea turtle' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Reptile' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  55% |████████/------|  654/1200 [4.3s elapsed, 3.5s remaining, 160.9 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Binoculars' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Snake' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  58% |████████|------|  698/1200 [4.7s elapsed, 3.4s remaining, 146.0 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Drill (Tool)' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  62% |█████████\\-----|  747/1200 [5.0s elapsed, 3.0s remaining, 150.5 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Refrigerator' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Lighthouse' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  74% |███████████\\---|  891/1200 [5.8s elapsed, 1.9s remaining, 170.7 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Spoon' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Kitchen utensil' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Fork' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  77% |███████████/---|  929/1200 [6.0s elapsed, 1.6s remaining, 172.2 samples/s]     "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Toothbrush' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  98% |██████████████\\| 1182/1200 [7.6s elapsed, 102.4ms remaining, 176.1 samples/s]  "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Stop sign' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Submarine sandwich' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Crutch' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 100% |███████████████| 1200/1200 [7.8s elapsed, 0s remaining, 171.6 samples/s]       \n"
     ]
    }
   ],
   "source": [
    "val_set.export(\n",
    "    export_dir=\"../openimages/val\",\n",
    "    dataset_type=fo.types.YOLOv5Dataset,\n",
    "    label_field=\"ground_truth\",  # 标签字段名\n",
    "    classes=new_classes,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2d787d42-46ed-4b83-80ba-c8b5419611ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  26% |████-------------| 158/600 [1.1s elapsed, 3.0s remaining, 151.7 samples/s]    "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Dice' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Tiara' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Magpie' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  36% |██████/----------| 217/600 [1.4s elapsed, 2.4s remaining, 167.0 samples/s]    "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Turkey' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  46% |███████|---------| 273/600 [1.7s elapsed, 1.9s remaining, 178.1 samples/s]    "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Pen' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  59% |██████████|------| 355/600 [2.1s elapsed, 1.4s remaining, 183.1 samples/s]    "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Porcupine' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Hedgehog' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  68% |███████████\\-----| 411/600 [2.4s elapsed, 1.1s remaining, 183.8 samples/s]    "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Tin can' not in provided classes\n",
      "  warnings.warn(msg)\n",
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Cucumber' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  86% |██████████████|--| 515/600 [3.0s elapsed, 464.5ms remaining, 184.7 samples/s] "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/lib/python3.10/site-packages/fiftyone/utils/yolo.py:1030: UserWarning: Ignoring object with label 'Tiger' not in provided classes\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 100% |█████████████████| 600/600 [3.4s elapsed, 0s remaining, 186.5 samples/s]      \n"
     ]
    }
   ],
   "source": [
    "\n",
    "test_set.export(\n",
    "    export_dir=\"../openimages/test\",\n",
    "    dataset_type=fo.types.YOLOv5Dataset,\n",
    "    label_field=\"ground_truth\",  # 标签字段名\n",
    "    classes=new_classes,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "168e20ec-c88b-4569-9396-269a8b99ed03",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22a3e0ce-2b83-442f-8cce-5db70db36a43",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fef203c-75de-46d7-991b-c99f315460cd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "20c78b74-3808-435a-b9cb-131b154eec8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "command = f\"\"\" \\\n",
    "rm -rf ../datasets/openimages/* && \\\n",
    "mkdir ../datasets/openimages/images && \\\n",
    "mkdir ../datasets/openimages/labels && \\\n",
    "mkdir ../datasets/openimages/images/train/ && \\\n",
    "mkdir ../datasets/openimages/images/val/ && \\\n",
    "mkdir ../datasets/openimages/images/test/ && \\\n",
    "mkdir ../datasets/openimages/labels/train/ && \\\n",
    "mkdir ../datasets/openimages/labels/val/ && \\\n",
    "mkdir ../datasets/openimages/labels/test/ && \\\n",
    "cp ../openimages/train/images/val/* ../datasets/openimages/images/train/ && \\\n",
    "cp ../openimages/val/images/val/* ../datasets/openimages/images/val/ && \\\n",
    "cp ../openimages/test/images/val/* ../datasets/openimages/images/test/ && \\\n",
    "cp ../openimages/train/labels/val/* ../datasets/openimages/labels/train/ && \\\n",
    "cp ../openimages/val/labels/val/* ../datasets/openimages/labels/val/ && \\\n",
    "cp ../openimages/test/labels/val/* ../datasets/openimages/labels/test/ \\\n",
    "\"\"\"\n",
    "!{command}\n",
    "#18：58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb6155d7-a545-4339-9cbb-e60250d28673",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ed00d695-a1c3-40a1-9677-a42c1c46d595",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 系统盘没空间了\n",
    "!mv /root/fiftyone/open-images-v6/ ../open-images-v6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21ccccf1-f1a9-49be-bfdc-d89f18817859",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163587ad-3100-4c3e-a8e9-71bf48387b32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
